package com.evildog.workspace.auth.grant;


import cn.hutool.core.util.StrUtil;
import com.evildog.workspace.common.result.Result;
import com.evildog.workspace.security.jwt.core.TokenUserInfo;
import com.evildog.workspace.system.api.feign.UserClient;
import com.evildog.workspace.system.api.vo.UserVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * PasswordTokenGranter
 */
@Component(PasswordTokenGranter.GRANT_TYPE)
@AllArgsConstructor
public class PasswordTokenGranter implements Granter {

    private final UserClient userClient;

    public static final String GRANT_TYPE = "password";

    @Override
    public TokenUserInfo grant(GrantParameter grantParameter) {
        String account = grantParameter.getAccount();
        String password = grantParameter.getPassword();


        TokenUserInfo tokenUserInfo = null;
        if (!StrUtil.hasEmpty(account, password)) {
            Result<UserVO> userVOResult = userClient.getUser(account, password);
            if (userVOResult.isSuccess()) {
                UserVO userVO = userVOResult.getData();
                tokenUserInfo = new TokenUserInfo();
                tokenUserInfo.setAvatar(userVO.getAvatar())
                        .setNickname(userVO.getNickname())
                        .setUserId(userVO.getId());
            }
        }
        return tokenUserInfo;
    }

}
